<?php
class reitingclass {

	function reitingclass() {
		global $setupdata, $mysqlclass;
		$this->setupdata=$setupdata;
		$this->mysqlclass=$mysqlclass;
	}

	function is_can_reiting($parent_id, $parent_type, $reit_name, $reit_check){
		$host = $_SERVER['REMOTE_ADDR'];
		if ($reit_check == 1){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."reiting` WHERE `ip`='".$host."' AND `parent_id`=".$parent_id." AND `parent_type`='".$parent_type."' AND `reit_name`='".$reit_name."'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0) return 1;
			else return 0;
		} else {
			if (isset($this->setupdata['modules']['reiting']['ver']) && ($this->setupdata['modules']['reiting']['ver'] == 2)){
				if ($this->get_checking() == 1){
					$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."reiting` WHERE `ip`='".$host."' AND `parent_id`=".$parent_id." AND `parent_type`='".$parent_type."' AND `reit_name`='".$reit_name."'", __LINE__, __FILE__);
					if (mysql_num_rows($sql) > 0) return 1;
				}
			}
			return 0;
		}
	}
	
	function add_reiting($page_id, $parent_id, $parent_type, $parent_name, $reit_value, $reit_name, $reit_check){
		$host = $_SERVER['REMOTE_ADDR'];
		if (intval($reit_value) > 0){
			$parent_id = intval($parent_id);
			$page_id = intval($page_id);
			if ($this->is_can_reiting($parent_id, $parent_type, $reit_name, $reit_check) == 0){
				if (isset($this->setupdata['modules']['reiting']['ver']) && ($this->setupdata['modules']['reiting']['ver'] == 2)){
					if (isset($_POST['reit_id'])){
						$n = $this->get_one_reiting_data($_POST['reit_id']);
						if (($n['name'] == $reit_name) && (md5("-Just".intval($_POST['reit_id'])."PageIt-".date("Ymd")) == $_SESSION['module_reiting_check'])){
							if ($this->check_raiting_value($_POST['reit_id'], $reit_value) === false) $reit_value = 0;
							$reit_name = trim($reit_name);
							$reit_name = addslashes($reit_name);
							$parent_name = trim($parent_name);
							$parent_name = addslashes($parent_name);	
							$parent_id = intval($parent_id);
							$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."reiting` (`id`,`page_id`,`parent_id`,`parent_type`,`parent_name`,`reit_name`,`reit_value`,`ip`) VALUES ('', ".$page_id.", ".$parent_id.", '".$parent_type."', '".$parent_name."', '".$reit_name."', ".$reit_value.", '".$host."')", __LINE__, __FILE__);
						}
					}
					return 0;
				} else {
					$reit_name = trim($reit_name);
					$reit_name = addslashes($reit_name);
					$parent_name = trim($parent_name);
					$parent_name = addslashes($parent_name);		
					$sql = $this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."reiting` (`id`,`page_id`,`parent_id`,`parent_type`,`parent_name`,`reit_name`,`reit_value`,`ip`) VALUES ('', ".$page_id.", ".$parent_id.", '".$parent_type."', '".$parent_name."', '".$reit_name."', ".$reit_value.", '".$host."')", __LINE__, __FILE__);
					return 0;
				}
			} else return 20;
		} else return 15;
	}
	
	function get_reiting(){
		global $treeclass;
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."reiting`", __LINE__, __FILE__);			
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result[$row['page_id']])){
				if ($row['page_id'] > 0) $result[$row['page_id']] = $treeclass->get_item_content($row['page_id']);
				else $result[$row['page_id']] = array();
			}
			$result[$row['page_id']]['elements'][$row['parent_id']]['name'] = $row['parent_name'];
			$result[$row['page_id']]['elements'][$row['parent_id']]['type'] = $row['parent_type'];
			if (!isset($result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']])){
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['count'] = 1;
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['suma'] = $row['reit_value'];
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['mid'] = $row['reit_value'];
			} else {
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['count'] = $result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['count'] + 1;
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['suma'] = $result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['suma'] + $row['reit_value'];
				$result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['mid'] = $result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['suma'] / $result[$row['page_id']]['elements'][$row['parent_id']]['elements'][$row['reit_name']]['count'];
			}
		}
		return $result;
	}
	
	function get_types(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `parent_type` FROM `".$this->setupdata['table_prefix']."reiting` GROUP BY `parent_type`", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result[$row['type']] = $row['type'];
		}
		return $result;
	}
	
	function reset_reiting(){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."reiting`", __LINE__, __FILE__);		
	}
	
	function reset_reiting_by_parent($parent){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."reiting` WHERE `parent_id`=".$parent, __LINE__, __FILE__);		
	}

	function reset_reiting_by_parent_and_name($parent, $name){
		$name = addslashes($name);
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."reiting` WHERE `parent_id`=".$parent." AND `reit_name`='".$name."'", __LINE__, __FILE__);
	}
	
	function get_reitings_by_parent_id_and_type($id, $type){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."reiting` WHERE `parent_id`=".$id." AND `parent_type`='".$type."'", __LINE__, __FILE__);			
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result[$row['reit_name']])){
				$result[$row['reit_name']]['count'] = 1;
				$result[$row['reit_name']]['suma'] = $row['reit_value'];
				$result[$row['reit_name']]['mid'] = $row['reit_value'];
			} else {
				$result[$row['reit_name']]['count'] = $result[$row['reit_name']]['count'] + 1;
				$result[$row['reit_name']]['suma'] = $result[$row['reit_name']]['suma'] + $row['reit_value'];
				$result[$row['reit_name']]['mid'] = $result[$row['reit_name']]['suma'] / $result[$row['reit_name']]['count'];
			}
		}		
		return $result;
	}
	
	function get_reitings_by_parent_id_type_and_name($id, $type, $name){
		$result = array();
		$result['count'] = 0;
		$result['suma'] = 0;
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."reiting` WHERE `parent_id`=".$id." AND `parent_type`='".$type."' AND `reit_name`='".$name."'", __LINE__, __FILE__);					
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			$result['name'] = $row['reit_name'];
			$result['count'] = $result['count'] + 1;
			$result['suma'] = $result['suma'] + $row['reit_value'];
			$result['mid'] = $result['suma'] / $result['count'];
		}
		return $result;
	}
	
	function array_sort($array, $key, $key2, $sort){ 
		foreach ($array as $k=>$v) $sort_values[$k] = $array[$k][$key][$key2]; 
		if ($sort == "-") asort($sort_values); 
		elseif ($sort == "+") arsort($sort_values); 
		reset ($sort_values); 
		while (list ($arr_key, $arr_val) = each ($sort_values)) $sorted_arr[] = $array[$arr_key]; 
		return $sorted_arr; 
	}

	function get_sorted_reitings_type_and_name($type, $name, $sortby, $sort){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT * FROM `".$this->setupdata['table_prefix']."reiting` WHERE `parent_type`='".$type."' AND `reit_name`='".$name."'", __LINE__, __FILE__);					
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) {
			if (!isset($result[$row['parent_id']][$row['reit_name']]['count'])) $result[$row['parent_id']][$row['reit_name']]['count'] = 0;
			if (!isset($result[$row['parent_id']][$row['reit_name']]['suma'])) $result[$row['parent_id']][$row['reit_name']]['suma'] = 0;
			$result[$row['parent_id']][$row['reit_name']]['name'] = $row['parent_name'];
			$result[$row['parent_id']][$row['reit_name']]['id'] = $row['parent_id'];
			$result[$row['parent_id']][$row['reit_name']]['count'] = $result[$row['parent_id']][$row['reit_name']]['count'] + 1;
			$result[$row['parent_id']][$row['reit_name']]['suma'] = $result[$row['parent_id']][$row['reit_name']]['suma'] + $row['reit_value'];
			$result[$row['parent_id']][$row['reit_name']]['mid'] = $result[$row['parent_id']][$row['reit_name']]['suma'] / $result[$row['parent_id']][$row['reit_name']]['count'];
		}
		$result = $this->array_sort($result, $name, $sortby, $sort);
		
		$results = array();
		foreach ($result as $k=>$v){
			$tmp['name'] = $v[$name]['name'];
			$tmp['id'] = $v[$name]['id'];
			$tmp['rating_name'] = $name;
			$tmp['rating_value']['suma'] = $v[$name]['suma'];
			$tmp['rating_value']['count'] = $v[$name]['count'];
			$tmp['rating_value']['mid'] = $v[$name]['mis'];
			$results[] = $tmp;
		}
		
		return $results;
	}
	
	function del_tree_elements($page_id){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."reiting` WHERE `page_id`=".$page_id, __LINE__, __FILE__);		
	}
	
	// NEW 2007.04.27
	
	function add_new_reiting($name){
		$name = addslashes(trim($name));
		$dat = date("YmdHis");
		$sql = "INSERT INTO `".$this->setupdata['table_prefix']."catalog` (`id`,`name`,`hide`,`pos`,`parent`, `depth`, `type`,`time_create`,`time_modif`,`lang`,`groupe`,`dlink`,`author`,`editor`)
				VALUES ('', '".$name."', '0', '0', '0', '0', 'module_reiting', '".$dat."', '".$dat."', '".$_SESSION['lang']."', '', '', '".$_SESSION['login_user']."', '".$_SESSION['login_user']."')";
		$this->mysqlclass->mysql_my_query($sql, __LINE__, __FILE__);
		return mysql_insert_id();
	}
	
	function get_new_reitings(){
		$result = array();
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id`, `name` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `type`='module_reiting'", __LINE__, __FILE__);
		while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) $result[$row['id']] = $row['name'];
		return $result;
	}
	
	function get_one_reiting_data($id){
		$id = intval($id);
		if ($id > 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`='".$id."' AND `type`='module_reiting'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				return mysql_fetch_array($sql, MYSQL_ASSOC);
			}
		}
		return "";
	}
	
	function set_checking($status){
		$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='module_reiting'", __LINE__, __FILE__);
		$this->mysqlclass->mysql_my_query("INSERT INTO `".$this->setupdata['table_prefix']."local_vars` (`id`,`name`,`value`) VALUES (NULL , 'module_reiting', '".$status."')", __LINE__, __FILE__);
	}

	function get_checking(){
		$sql = $this->mysqlclass->mysql_my_query("SELECT `value` FROM `".$this->setupdata['table_prefix']."local_vars` WHERE `name`='module_reiting'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0){
			$row = mysql_fetch_array($sql, MYSQL_ASSOC);
			if ($row['value'] == '1') return 1;
			else return 0;
		}
		return 0;
	}
	
	function del_new_reiting($id){
		$id = intval($id);
		if ($id > 0){
			$sql = $this->mysqlclass->mysql_my_query("SELECT `name` FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id." AND `type`='module_reiting'", __LINE__, __FILE__);
			if (mysql_num_rows($sql) > 0){
				$row = mysql_fetch_array($sql, MYSQL_ASSOC);
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."catalog` WHERE `id`=".$id." AND `type`='module_reiting'", __LINE__, __FILE__);
				$this->mysqlclass->mysql_my_query("DELETE FROM `".$this->setupdata['table_prefix']."reiting` WHERE `reit_name`='".$row['name']."'", __LINE__, __FILE__);				
			}
		}
	}
	
	function start_raiting_sessios($id){
		unset($_SESSION['module_reiting_check']);
		$_SESSION['module_reiting_check'] = md5("-Just".$id."PageIt-".date("Ymd"));
	}

	function get_data($id){
		global $treeclass;
		$dat = $treeclass->get_form_elem_data_guest($id);
		return $dat;
	}
	
	function check_raiting_value($id, $value){
		$id = intval($id);
		$value = intval($value);
		$sql = $this->mysqlclass->mysql_my_query("SELECT `id` FROM `".$this->setupdata['table_prefix']."object_data` WHERE `parent`=".$id." AND `value`='".$value."'", __LINE__, __FILE__);
		if (mysql_num_rows($sql) > 0) return true;
		else return false;
	}
}
?>